package com.cmc.cloud.cmclink.doc.api.document;

import com.cmc.cloud.cmclink.doc.api.document.rep.DgCommodityRespDto;
import com.cmc.cloud.cmclink.doc.api.document.req.DgCommodityReqDto;
import com.cmc.cloud.cmclink.doc.convert.DgCommodityConvert;
import com.cmc.cloud.cmclink.doc.entity.DgCommodityDO;
import com.cmc.cloud.cmclink.doc.entity.DgCommodityPsnDO;
import com.cmc.cloud.cmclink.doc.mapper.DgCommodityMapper;
import com.cmc.cloud.cmclink.doc.mapper.DgCommodityPsnMapper;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 特种货危险品品名查询
 *
 * @author lzp
 */
@RestController
@Validated
public class DgCommodityApiImpl implements DgCommodityApi {

    @Resource
    private DgCommodityMapper dgCommodityMapper;

    @Resource
    private DgCommodityPsnMapper dgCommodityPsnMapper;

    @Override
    public CommonResult<List<DgCommodityRespDto>> queryDgCommodity(DgCommodityReqDto dgCommodityReqDto) {
        LambdaQueryWrapperX<DgCommodityDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        lambdaQueryWrapperX.likeIfPresent(DgCommodityDO::getUnNo, dgCommodityReqDto.getUnNo());
        List<DgCommodityDO> dgCommodityDOList = dgCommodityMapper.selectList(lambdaQueryWrapperX);
        if (CollectionUtils.isEmpty(dgCommodityDOList)) {
            return CommonResult.success(Collections.emptyList());
        }
        return CommonResult.success(DgCommodityConvert.INSTANCE.convertRpcDgCommodityRespDto(dgCommodityDOList));
    }

    @Override
    public CommonResult<List<String>> queryDgCommodityPsn(Long id) {
        if (id == null) {
            return CommonResult.success(Collections.emptyList());
        }
        LambdaQueryWrapperX<DgCommodityPsnDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        lambdaQueryWrapperX.eq(DgCommodityPsnDO::getMainId, id);
        List<DgCommodityPsnDO> dgCommodityPsnDOList = dgCommodityPsnMapper.selectList(lambdaQueryWrapperX);
        if (CollectionUtils.isEmpty(dgCommodityPsnDOList)) {
            return CommonResult.success(Collections.emptyList());
        }
        return CommonResult.success(dgCommodityPsnDOList.stream().map(DgCommodityPsnDO::getPsn).collect(Collectors.toList()));
    }
}
